By
yusijia
June 04 2016
Updated:June 04 2016
参考:http://bbs.itheima.com/thread-90856-1-1.html
一、扫描控制台输入 当通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象。如果要获取输入的内容,则只需要调用Scanner的nextLine()方法即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 * 扫描控制台输入 * * @author leizhimin 2009-7-24 11:24:47 */ public class TestScanner { public static void main (String[] args) { Scanner s = new Scanner(System.in); System.out.println("请输入字符串:" ); while (true ) { String line = s.nextLine(); if (line.equals("exit" )) break ; System.out.println(">>>" + line); } } } 请输入字符串: 234 >>>234 wer >>>wer bye >>>bye exit Process finished with exit code 0
二、构建Scanner的对象很方便。 可以从字符串(Readable)、输入流、文件等等来直接构建Scanner对象,有了Scanner了,就可以逐段(根据正则分隔式)来扫描整个文本,并对扫描后的结果做想要的处理。
三、Scanner分隔文本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 使用默认的空格分隔符: public static void main (String[] args) throws FileNotFoundException { Scanner s = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las" ); while (s.hasNext()) { System.out.println(s.next()); } } 123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las Process finished with exit code 0 将注释行去掉,使用空格或逗号或点号作为分隔符,输出结果如下: 123 asdf sd 45 789 sdf asdfl sdf sdfl asdf asdfkl las Process finished with exit code 0
四、一大堆API函数,实用的没几个 下面这几个相对实用:
1 2 3 4 5 6 7 8 9 10 delimiter() 返回此 Scanner 当前正在用于匹配分隔符的 Pattern。 hasNext() 判断扫描器中当前扫描位置后是否还存在下一段。(原APIDoc的注释很扯淡) hasNextLine() 如果在此扫描器的输入中存在另一行,则返回 true 。 next() 查找并返回来自此扫描器的下一个完整标记。 nextLine() 此扫描器执行当前行,并返回跳过的输入信息。
五、逐行扫描文件,并逐行输出 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public static void main (String[] args) throws FileNotFoundException { InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java" )); Scanner s = new Scanner(in); while (s.hasNextLine()){ System.out.println(s.nextLine()); } } package own; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; import com.verisign.uuid.UUID; * @author wangpeng * */ public class AutoSubmit { * @param args * @throws Exception */ public static void main (String[] args) throws Exception { ...在此省略N行 Process finished with exit code 0